package uniqueidx

import (
	"fmt"
	"os"
	"sync/atomic"
	"time"
	// "mall/common/randomx"
)

type SnPrefix string

const (
	SN_PREFIX_GEM_ORDER    SnPrefix = "G" // 充值订单前缀
	SN_PREFIX_COMMON_ORDER SnPrefix = "O" // 普通实物订单前缀

	SN_PREFIX_GEM                SnPrefix = "GEM" // 余额支付流水记录前缀
	SN_PREFIX_THIRD_PART_PAYMENT SnPrefix = "PMT" // 第三方支付流水记录前缀

	SN_PREFIX_GEM_REFUND        SnPrefix = "GRF" // 余额支付退款记录前缀
	SN_PREFIX_THIRD_PART_REFUND SnPrefix = "RF"  // 第三方支付退款记录前缀
)

// 售后sn前缀
const (
	SN_PREFIX_Refund SnPrefix = "TK" // 退货退款售后前缀
	SN_PREFIX_Return SnPrefix = "TH" // 退货售后前缀
	SN_PREFIX_Barter SnPrefix = "HH" // 换货售后前缀
)

// func GenSn(snPrefix SnPrefix) string {
// 	return fmt.Sprintf("%s%s%s", snPrefix, time.Now().Format("20060102150405"), randomx.Krand(11, randomx.KC_RAND_KIND_NUM))
// }

func GenSn(snPrefix SnPrefix) string { // #24.10.06#  Todo: review
	t := time.Now()
	s := t.Format("20060102150405")

	m := t.UnixNano()/1e6 - t.UnixNano()/1e9*1e3
	ms := sup(m, 3)

	p := os.Getpid() % 1000
	ps := sup(int64(p), 3)

	var num int64
	i := atomic.AddInt64(&num, 1)
	r := i % 10000
	rs := sup(r, 3)

	n := fmt.Sprintf("%s%s%s%s%s", snPrefix, s, ms, ps, rs)

	return n
}

func sup(i int64, n int) string {
	m := fmt.Sprintf("%d", i)
	for len(m) < n {
		m = fmt.Sprintf("0%s", m)
	}

	return m
}
